/**
 * ini
 */
$(function() {
	assessmentAndStrategy();
	css_BreakWordAndBreakAll();
	videoLeay();
	backToTop();
	lessonVideo();
})

  
//showText、hideText、displayText用于
// Course Introduction中隐藏显示的
/**
 * 显示全部文本
 */
function showText(index) {
	var divHide = $("#course-introd-cont-id" + index).html();
	var $divShow = $(".course-introd-cont").eq(index);
	var $p = $("div", $divShow).eq(0);
	$p.html(divHide+'<em class="introd-arrow-hover project-ico blur" onClick="hideText('+ index + ');"></em>');
	$divShow.css("height", $p.outerHeight() + "px");
	findDescendants();
}


/**
 * 课程简介表单显示处理
 */
function findDescendants() {
	var $td = $(".tableclass td");
	for (var j = 0; j < $td.length; j++) {
		$td.eq(j).css("border", "1px solid ");
	}
}


/**
 * 课程简介
 * 把文本全部显示，但是会把页面拉长
 */
function hideText(index) {
	var $divShow = $(".course-introd-cont").eq(index);
	$divShow.css("height", "58px");
	var divH = $divShow.height();
	var $p = $("div", $divShow).eq(0);
	var emImg='...<em class="introd-arrow-hover project-ico" onclick="showText('+index+');"></em>';
	while($p.outerHeight() > divH){
	   $p.html($p.text().replace(/([a-zA-Z0-9\\_]+|\W)(\.\.\.)?$/,emImg));
	};
	
	 var str_p=$.trim($("#course-introd-cont-id"+index).find("p").text());
	 if(str_p==''){
	    str_p=$.trim($("#course-introd-cont-id"+index).text());
	 }
	 em($p,str_p,emImg);
	 
}


/**
 * 课程简介 把文本全部显示，但是会把页面拉长
 */
function displayText() {
	$(".course-introd-cont").each(function(i) {
		var divH = $(this).height();
		var $p = $("div", $(this));
		var emImg='...<em class="introd-arrow-hover project-ico" onclick="showText('+i+');"></em>';
		while($p.outerHeight() > divH) {
		     $p.html($p.text().replace(/(\s)*([a-zA-Z0-9\\_]+|\W)(\.\.\.)?$/,emImg));
		 };
		 
		var strText=$.trim($("#course-introd-cont-id"+i).text());
		em($p,strText,emImg);
	});
}

//判断  ...
function em($p,strText,emImg){
	if($.trim(arguments[0].text()).indexOf('...')==0){
		arguments[0].html(arguments[1].substring(0,150)+arguments[2]);
	 }
}

	
/**
 * 先修课程,主讲人简介 弹框显示的高度不同，需要写不同的
 */
function alertDivDisplayText(divClass,emClass){
	$("."+divClass).each(function(i){
		  var divH = $("."+divClass).height();
		  var $p = $("div", $(this)).eq(i);
		  while($p.outerHeight() > divH){
		      $p.html($p.text().replace(/(\s)*([a-zA-Z0-9\\_]+|\W)(\.\.\.)?$/,'...<em class="'+emClass+' project-ico"></em>'));
		   };
		});   
	}
	
	
/**
 * ASSESSMENT STRATEGY
 * 分数成绩
 * 转换整型
 */
function assessmentAndStrategy(){
	var $decimal = $(".ininum");
	var $colorFullmartk = $(".fullmark-ul li");
	var num, arr = [];
	$decimal.each(function(i) {
		num = parseFloat($decimal.eq(i).text())
		$decimal.eq(i).text(num);
	})
	// ===分数进度圆角
	$colorFullmartk.each(function(i) {
		if ($colorFullmartk.eq(i).text() <= 0) {
			$colorFullmartk.eq(i).hide();
		} else {
			arr.push(i);
		}
	})
	$colorFullmartk.eq(arr[0]).addClass("leftradius");
	$colorFullmartk.eq(arr[arr.length - 1]).addClass("rightradius");
}

	
/**
 * 小节视频弹框提示
 */
function videoLeay(){
	 $("#video-ul li:gt(1)").live("click", function() {
		layer.open({
			title : ' ',
			icon : 0,
			content : zLocale.creatcoursehome_trial_watch,
			shade : .6,
			skin : 'layui-layer-zhs',
			btn : [ zLocale.public_confim ]
		// 按钮
		});
	$(".layui-layer-padding").css("padding-left", "95px");//
	 });
}
	

/**
 * Prerequisites ,Course Introduction
 * css样式 word-break:break-all
 *        word-wrap:break-word;
 */
function css_BreakWordAndBreakAll() {
	var $str = $("#institution");
	var $text_course = $(".text_course");
	$text_course.addClass("breakword");
	if ($str.text().indexOf(" ") >= 0) {
		$str.addClass("breakword");
	} else {
		$str.addClass("bareakall");
	}
}


/**
 * 章、节、小节
 */
$(function(){
	  $.ajax({
			type : 'post',
			url : '/course/courseHome/courseHomeInfoToTwo',
			data : {
				"courseId" : $("#courseId").val()
			},
			dataType : 'json',
			success : function(result) {
				
            //统计
		    $(".onlinelearning-l .number").text(result.count.chaptersCount);
			$(".onlinelearning-c .number").text(result.count.videosCount);
			if (result.count.videosCountTime > 59) {
				$(".onlinelearning-r .number").text(Math.floor(result.count.videosCountTime / 60)).next().text(zLocale.creatcoursehome_fenzhong);
			} else {
				$(".onlinelearning-r .number").text(result.count.videosCountTime).next().text(zLocale.creatcoursehome_miao);
			}

			lessonVideoList(result);

			courseSchedule(result);

			syllabus(result);
			     	    
				    	
                       
				}
		})
})
	





/**
 * SYLLABUS 教学大纲
 */ 
function syllabus(result){
	for(var i=0;i<result.chapterList.length;i++){
		  var objs=result.chapterList[i];
		  var obj_ChapterList="<li class='chapterList chapterInfoClass'>"
    	        +"<div class='chaptertitlist-p'>"+zLocale.creatcoursehome_zhang+"&nbsp;"+(i+1)+":&nbsp;"+objs.name+"</div>"
    	        +"<p>"+objs.description+"</p>"
                +"<ul class='section-title-ul'>"
                var objj_lessonList='';
                for(var j=0;j<objs.lessonList.length;j++){
                 var objss=objs.lessonList[j];
					  objj_lessonList+="<li><h3 class='section-title'>"+zLocale.creatcoursehome_jie+"&nbsp;"+(j+1)+":&nbsp;"+objss.name+"</h3>"
                     +"<p>"+objss.introduction+"</p>" 
                     +"</li>"
                 }
                $("#syllabus-ul_id").append(obj_ChapterList+objj_lessonList+"</ul></li>");
	    }
	         //处理table表单无用样式
			$(".chapterInfoClass span,table").css("color", "#959595");
			$(".chapterInfoClass td").css("border", "1px solid ");
}

/**
 * courseSchedule 课程表
 */
function courseSchedule(result){
	for(var a=0;a<result.chapterList.length;a++){
		  var objs=result.chapterList[a];
		  if(result.mapCountVideo[objs.id]==undefined){
			  result.mapCountVideo[objs.id]=0;
		  }
		  var obj_courseSchedule="<div class='teachindaytime-div'>"
  	                +"<span class='crirclebig-bg'><span class='crirclefont'>"
          	        +""+objs.limitDay+zLocale.creatcoursehome_tian+"</span></span>"
	    	        +"<div class='teachindaytime-content'>" 
	      		    +"<div class='chaptertit-p'>"+zLocale.creatcoursehome_zhang+"&nbsp;"+(a+1)+""
                	+"<span class='minumun-tip'>("+zLocale.creatcoursehome_zuidixueshi+""+objs.studyHour+")</span></br>"+"<li title='"+objs.name+"'>"
                	+objs.name+"</li>"+"</div>"
					+"<ul class='teachindaytime-ul'>"
//					    <c:forEach items="${chapterInfo.lessonList}" var="lesson" varStatus="sstatus" >
//              	      <li title="${lesson.name}">${sstatus.index+1}:${lesson.name }</li>
//              	    </c:forEach>
		 var obj_courseSchedule_Down="</ul>"
	                    +"<div class='videodataico-div'>"
	                    +"<span class='videoico-span fl'><span  class='videoico fl' title='"+zLocale.creatcoursehome_video+"'></span>" 
	                    +"<span class='videodatafont fl'>"
	                    +""+isNull(result.mapCountVideo[objs.id])+"</span></span>"
	                    +"<span class='dataico-span fl'><span class='dataico fl' title='"+zLocale.creatcoursehome_zhangtaolun+"'>" 
	                    +"</span><span class='videodatafont fl'>"
	                    +""+isNull(result.mapCountBbs[objs.id])+"</span></span>"
	                    +"</div></div>"
		                +"<div class='chapter-div clearfix'>"
	                    +"<span class='chapter-font'>"+zLocale.creatcoursehome_zhangceshi+"&nbsp;"+(a+1)+"</span>"
	                    +"<span class='chapter-ico'></span>"
	                    +"</div>"
	                    
	                var objj_lessonName="";
		  
	                for(var k=0;k<objs.lessonList.length;k++){
		                 var objss=objs.lessonList[k];
					     objj_lessonName+="<li title='"+objss.name+"'>"+(k+1)+":"+objss.name+"</li>"
		            }
	                
	                $("#teachinplanview-li").append(obj_courseSchedule+objj_lessonName+obj_courseSchedule_Down);
            }
				
	            var $teachindaytime_div = $(".teachindaytime-div");
				var divRight = "teachindaytime-div-right";
				var divLeft = "teachindaytime-div-left";
				for (var i = 0; i < $teachindaytime_div.length; i++) {
					if ((i & 1) == 0) {
						$teachindaytime_div.eq(i).addClass(divRight);
					} else {
						$teachindaytime_div.eq(i).addClass(divLeft);
					}
				}
			   
				var end="<div class='teachinfinalexam-div'><span class='crirclebig-bg'><span class='crirclefont'>"+zLocale.creatcoursehome_qimo+"</br>"+zLocale.creatcoursehome_kaoshi+"</span></span>"
			      +"<span class='crircle-border'></span></div><div class='teachinstartend-div'>"
			      +"<span class='crirclebig-bg'><span class='crirclefont'>"+zLocale.creatcoursehome_jieshu+"</br>...</span></span></div>"
			    
			      $("#teachinplanview-li").append(end);
}


/**
 * 小节视频
 */
function lessonVideoList(result){
	$(".videoLi-class a").live("click",function(){});//事件绑定
	for(var y=0;y<result.lessonVideoList.length;y++){
		 var objs=result.lessonVideoList[y];
		 if(y>1){
			 objs.videoUrl='';
		 }	
	     var objs_video="<li class='videoLi-class'><a href='javascript:void(0);'>"
			    +"<img width='150' height='84' src='"+objs.videoImg+"' title='"+objs.lessonName+"' alt='"+objs.lessonName+"'/>"
		        +"</a><input type='hidden' class='videoinput-class' value='"+objs.videoId+"' ></li>"
	     $("#video-ul").append(objs_video);
	 }
	     //左右滑动
		$("#video-ul").scroll_left_right({
			srcoll_obj:$("#video-ul"),
			panel_elem: 4,
			arrow_left: "#sliderprev",
			arrow_right: "#slidernext",
			edge_scroll:false
		});
	
}

function isNull(obj){
	 return (obj =='undefined' || obj == undefined || obj=="" || obj == null) ? 0 : obj;
}
